home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13121 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: hillres151.cc.purdue.edu!pacman
  2. From: pacman@hillres151.cc.purdue.edu (PacMan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can you printf a long
  5. Date: 4 Apr 1996 19:09:24 GMT
  6. Organization: prohibited
  7. Message-ID: <4k16p4$p28@mozo.cc.purdue.edu>
  8. References: <4ju8o1$dc3@news.netam.net>
  9. NNTP-Posting-Host: hillres151.cc.purdue.edu
  10.  
  11. The Bowling Green Connection <bgc@alpha.netam.net> wrote:
  12. :I had some trouble with this code:
  13. :(I'm using gcc on Digital Unix)
  14. :
  15. [snip]
  16. :    long i;
  17. [snip]
  18. :    printf("%f\n", i);
  19. [snip]
  20.  
  21. %f is for floats, not longs.
  22.  
  23. :But when I changed %f to %d, the numbers printed correctly.
  24.  
  25. %d is better than %f, but what you should have done was use %ld. The l
  26. means "long". Similarly, an h can be used to mean short, as in %hd.
  27.  
  28. :I thought that %d had the same limitations as an int..that is,
  29. There you are correct. %d is for ints.
  30.  
  31. :it could only print about 4 bits of information, whereas a long
  32. An int is supposed to be the natural word size of the machine. On 32-bit
  33. machines, it should be 32 bits. This is also OS/compiler dependent-- old
  34. MSloss compilers still use 16-bit ints on 32-bit machines. In any case,
  35. though, %d is the correct printf/scanf specifier for ints, and %ld for
  36. longs.
  37.  
  38. :is capable of more (8 or 16 bits),
  39. longs might be bigger than ints, or they might not. (K&R2 p.196)
  40.  
  41. :                                   so wouldn't %f (float) be able
  42. :to better handle those long numbers?
  43. Here's where you really lost it: long,int,short,and char are all integer
  44. types. float and double are something completely different. Don't mix them
  45. unless you're sure you know what you're doing.
  46.  
  47. :                                      And why did %d work
  48. :correctly, even up to 634,000 (that's when my disk space ran out.. :))
  49. %d worked correctly because you happened to be on a system whose longs are
  50. the same as its ints. %ld would be the "most correct" conversion string.
  51.  
  52. :
  53. :Well, then I changed the "long i" declaration to "int i", and changed %f 
  54. :to %d, and I got the SAME results!  The numbers went up to 634,000!
  55. :I didn't think an int could handle this much!
  56. On most systems, it can.
  57.  
  58. :
  59. :Another strange thing is, I did a sizeof(int) and a sizeof(long), and they
  60. :BOTH returned a 4!!!  That can't be right, can it??!
  61. It can. see again K&R2 p.196 and also p257 and your system's <limits.h>
  62. -- 
  63. Alan Curry
  64. -----BEGIN GEEK CODE BLOCK-----
  65. Version: 3.1
  66. GCS d? s++:-- a--- C+++ UL++++ P+ L+++>++++ E--- W-- N++ o K? w--- O? M--
  67. V?  PS+ PE+ Y+ PGP- t* 5++ X+++ R- tv++ b- DI- D++ G+++ !e h! r-->+++ y?
  68. ------END GEEK CODE BLOCK------
  69.